home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK2.toast / Development Kits (Disc 2) / QuickTime / Sample Code / QT QuickDraw 3D tracks / Code / NameAttributeHandler / NameAttributeHandler.c next >
Encoding:
C/C++ Source or Header  |  1997-02-26  |  4.6 KB  |  226 lines  |  [TEXT/CWIE]

  1. #include <QD3D.h>
  2. #include <QD3DSet.h>
  3.  
  4. #include <QD3DIO.h>
  5. #include <QD3DString.h>
  6. #include <QD3DGeometry.h>
  7.  
  8. #include <stdlib.h>
  9.  
  10. #include "NameAttributeHandler.h"
  11.  
  12. static TQ3ObjectClass    gNameAttributeClass = NULL;
  13.  
  14. /**********************************************************************************************
  15.  *
  16.  *                                        NAME custom attribute
  17.  *
  18.  **********************************************************************************************/
  19.  
  20.  
  21. /*
  22.  * Utility function to add a name on an shape object, geometry object, or attribute set
  23.  */
  24.  
  25. TQ3Status    SetName(TQ3Object object, char    *name)
  26. {
  27.     TQ3StringObject    string = NULL;
  28.     TQ3AttributeSet    set = NULL;
  29.     TQ3Status        status = kQ3Success;
  30.     
  31.     if( Q3Object_IsType(object, kQ3SharedTypeShape) == kQ3True ) {
  32.         
  33.         string = Q3CString_New(name);
  34.         
  35.         if( string == NULL) {
  36.             status = kQ3Failure;
  37.             goto cleanExit;
  38.         }
  39.             
  40.         if( Q3Object_IsType(object, kQ3ShapeTypeGeometry) == kQ3True ) {
  41.             
  42.             Q3Geometry_GetAttributeSet(object, &set);
  43.             
  44.             if( set == NULL ) {
  45.                 set = Q3AttributeSet_New();
  46.                 if( set == NULL ) {
  47.                     status = kQ3Failure;
  48.                     goto cleanExit;
  49.                 }
  50.                 Q3Geometry_SetAttributeSet(object, set);
  51.             }
  52.         } else {
  53.             Q3Shape_GetSet(object, &set);
  54.             
  55.             if( set == NULL ) {
  56.                 set = Q3Set_New();
  57.                 if( set == NULL ) {
  58.                     status = kQ3Failure;
  59.                     goto cleanExit;
  60.                 }
  61.                 Q3Shape_SetSet(object, set);
  62.             }
  63.         }
  64.             
  65.         if( Q3Set_Add(set, kElementTypeName, &string) == kQ3Failure ) {
  66.             status = kQ3Failure;
  67.             goto cleanExit;
  68.         }
  69.     } else if( Q3Object_IsType(object, kQ3SharedTypeSet) == kQ3True ) {
  70.         string = Q3CString_New(name);
  71.         
  72.         if( string == NULL) {
  73.             status = kQ3Failure;
  74.             goto cleanExit;
  75.         }
  76.         
  77.         if( Q3AttributeSet_Add(object, kElementTypeName, &string) == kQ3Failure ) {
  78.             status = kQ3Failure;
  79.             goto cleanExit;
  80.         }
  81.     } else 
  82.         status = kQ3Failure;
  83.         
  84. cleanExit:
  85.     if( string )
  86.         Q3Object_Dispose(string);
  87.     if( set )
  88.         Q3Object_Dispose(set);
  89.     return status;
  90. }
  91.  
  92. /*
  93.  * Static Functions
  94.  */
  95.  
  96. static TQ3Status NameAttribute_Traverse(
  97.     TQ3Object                unused,
  98.     TQ3StringObject            *stringObject,
  99.     TQ3ViewObject            view)
  100. {
  101.     (void) unused;
  102.     
  103.     if (stringObject == NULL || *stringObject == NULL)
  104.         return kQ3Success;
  105.  
  106.     Q3View_SubmitWriteData(view,0,0,0);
  107.  
  108.     if (Q3Object_Submit( *stringObject, view) == kQ3Failure)
  109.         return kQ3Failure;
  110.  
  111.     return kQ3Success;
  112. }
  113.  
  114. static TQ3Status NameAttribute_ReadData(
  115.     TQ3SetObject            attributeSet,
  116.     TQ3FileObject            file)
  117. {
  118.     TQ3StringObject        stringObject;
  119.     TQ3Status            status;
  120.     
  121.     stringObject = Q3File_ReadObject(file);
  122.         
  123.     status = Q3Set_Add(attributeSet, kElementTypeName, &stringObject);
  124.  
  125.     if (status == kQ3Failure)
  126.         Q3Object_Dispose(stringObject);
  127.  
  128.     /*
  129.         Note that the string object has a reference count of 1,
  130.         which will be taken care of in the dispose
  131.     */
  132.     return status;
  133. }
  134.  
  135. static TQ3Status NameAttribute_CopyAdd(
  136.     TQ3StringObject    *src,
  137.     TQ3StringObject    *dst)
  138. {
  139.     *dst = Q3Shared_GetReference(*src);
  140.     if (*dst == NULL) 
  141.         return kQ3Failure;
  142.     
  143.     return kQ3Success;
  144. }
  145.  
  146. static TQ3Status NameAttribute_CopyReplace(
  147.     TQ3StringObject    *src,
  148.     TQ3StringObject    *dst)
  149. {
  150.     TQ3StringObject    tempString;
  151.     
  152.     /*
  153.         It is always good form to get a reference first,
  154.         in case src and dst point to the same object
  155.     */
  156.     
  157.     tempString = Q3Shared_GetReference(*src);
  158.     if (tempString == NULL) 
  159.         return kQ3Failure;
  160.  
  161.     if( *src )
  162.         Q3Object_Dispose( *src );
  163.     
  164.     *dst = tempString;
  165.  
  166.     return kQ3Success;
  167. }
  168.  
  169. static TQ3Status NameAttribute_Delete(
  170.     TQ3StringObject    *stringObject)
  171. {
  172.     if(*stringObject)
  173.         Q3Object_Dispose(*stringObject);
  174.     return kQ3Success;
  175. }
  176.  
  177. TQ3Status NameAttribute_Unregister(
  178.     void)
  179. {
  180.     if ( gNameAttributeClass != NULL )
  181.         return    Q3ObjectClass_Unregister(gNameAttributeClass);
  182.         
  183.     return kQ3Failure;
  184. }
  185.  
  186. /*
  187.  * NameAttribute_MetaHandler
  188.  */
  189. static TQ3FunctionPointer NameAttribute_MetaHandler(
  190.     TQ3MethodType        methodType)
  191. {
  192.     switch (methodType)
  193.     {
  194.         case kQ3MethodTypeObjectTraverse:
  195.             return (TQ3FunctionPointer) NameAttribute_Traverse;
  196.         case kQ3MethodTypeObjectReadData:
  197.             return (TQ3FunctionPointer) NameAttribute_ReadData;
  198.         case kQ3MethodTypeElementCopyAdd:
  199.         case kQ3MethodTypeElementCopyGet:
  200.         case kQ3MethodTypeElementCopyDuplicate:
  201.             return (TQ3FunctionPointer) NameAttribute_CopyAdd;
  202.         case kQ3MethodTypeElementCopyReplace:
  203.             return (TQ3FunctionPointer) NameAttribute_CopyReplace;
  204.         case kQ3MethodTypeElementDelete:
  205.             return (TQ3FunctionPointer) NameAttribute_Delete;
  206.         default:
  207.             return (TQ3FunctionPointer) NULL;
  208.     }
  209. }
  210.  
  211. /*
  212.  * NameAttribute_Register
  213.  */
  214. TQ3Status NameAttribute_Register(
  215.     void)
  216. {
  217.     gNameAttributeClass = 
  218.         Q3ElementClass_Register(
  219.             kElementTypeName,
  220.             "NameAttribute",
  221.             sizeof(TQ3StringObject),
  222.             NameAttribute_MetaHandler);
  223.  
  224.     return (gNameAttributeClass == NULL ? kQ3Failure : kQ3Success);
  225. }
  226.